package com.ms.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ms.dao.VolunteerMapper;
import com.ms.entity.Volunteer;
import com.ms.service.inter.VolunteerService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * 志愿者服务实现类 - 实现VolunteerService接口
 */
@Service
@Transactional
public class VolunteerServiceImpl extends ServiceImpl<VolunteerMapper, Volunteer> implements VolunteerService {

    @Autowired
    private VolunteerMapper volunteerMapper;

    /**
     * 保存志愿者申请
     * @param volunteer 志愿者对象
     * @return 保存结果
     */
    @Override
    public boolean save(Volunteer volunteer) {
        try {
            // 修改：移除手动生成ID的逻辑，使用数据库自增
            int result = volunteerMapper.insert(volunteer);
            return result > 0;
        } catch (Exception e) {
            throw new RuntimeException("保存志愿者申请失败: " + e.getMessage(), e);
        }
    }

    /**
     * 根据ID查找志愿者申请
     * @param volunteerID 志愿者ID - 修改：改为 Integer
     * @return 志愿者对象
     */
    @Override
    public Volunteer findById(Integer volunteerID) {
        try {
            return volunteerMapper.selectById(volunteerID);
        } catch (Exception e) {
            throw new RuntimeException("根据ID查找志愿者申请失败: " + e.getMessage(), e);
        }
    }

    /**
     * 根据用户ID查找志愿者申请
     * @param userID 用户ID
     * @return 志愿者对象
     */
    @Override
    public Volunteer findByUserId(Integer userID) {
        try {
            // 修改：直接传递 Integer 参数
            return volunteerMapper.selectByUserId(userID);
        } catch (Exception e) {
            throw new RuntimeException("根据用户ID查找志愿者申请失败: " + e.getMessage(), e);
        }
    }

    /**
     * 获取所有志愿者申请
     * @return 志愿者申请列表
     */
    @Override
    public List<Volunteer> findAll() {
        try {
            return volunteerMapper.selectList(null);
        } catch (Exception e) {
            throw new RuntimeException("获取所有志愿者申请失败: " + e.getMessage(), e);
        }
    }

    /**
     * 更新志愿者申请
     * @param volunteer 志愿者对象
     * @return 更新结果
     */
    @Override
    public boolean update(Volunteer volunteer) {
        try {
            int result = volunteerMapper.updateById(volunteer);
            return result > 0;
        } catch (Exception e) {
            throw new RuntimeException("更新志愿者申请失败: " + e.getMessage(), e);
        }
    }

    /**
     * 删除志愿者申请
     * @param volunteerID 志愿者ID - 修改：改为 Integer
     * @return 删除结果
     */
    @Override
    public boolean delete(Integer volunteerID) {
        try {
            int result = volunteerMapper.deleteById(volunteerID);
            return result > 0;
        } catch (Exception e) {
            throw new RuntimeException("删除志愿者申请失败: " + e.getMessage(), e);
        }
    }

    /**
     * 根据用户ID删除志愿者申请
     * @param userID 用户ID
     * @return 删除结果
     */
    @Override
    public boolean deleteByUserId(Integer userID) {
        try {
            // 修改：直接传递 Integer 参数
            int result = volunteerMapper.deleteByUserId(userID);
            return result > 0;
        } catch (Exception e) {
            throw new RuntimeException("根据用户ID删除志愿者申请失败: " + e.getMessage(), e);
        }
    }

    /**
     * 检查用户是否已经提交过志愿者申请
     * @param userID 用户ID
     * @return 是否已申请
     */
    @Override
    public boolean hasUserApplied(Integer userID) {
        try {
            // 修改：直接传递 Integer 参数
            return volunteerMapper.existsByUserId(userID);
        } catch (Exception e) {
            throw new RuntimeException("检查用户志愿者申请状态失败: " + e.getMessage(), e);
        }
    }

    // 删除 generateVolunteerId 方法，因为现在使用自增ID
}